/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.runtime.Event.*;
	
	import flash.events.Event;
	
	import mx.controls.CheckBox;
	/**
	 * 单项选择框组件
	 * 在MXML文件中使用：
	 * <p><EDICheckBox field="important"  width="100"  visible="true"  fieldType="boolean"  label="重要" fieldLength="1"  id="ImportantChk"  table="MailItem" height="20"  primaryKey="false"  y="41" x="533"></EDICheckBox></p>
	 * 
	 */	
	public class EDICheckBox extends CheckBox implements IEDIComponent
	{
		private var _field:String;
		private var _labelField:String; 
		private var _fieldType:String;
		private var _fieldLength:int;		
		private var _table:String;
		private var _primaryKey:String="";
		/**
		 * 构造函数，注册选择框发生变化触发的处理函数
		 * 
		 */		
		public function EDICheckBox()
		{
			super();
			this.addEventListener(Event.CHANGE,changeHandler);
		}
		/**
		 * 实现IEDIComponent接口的属性
		 * @param value
		 * 
		 */		
		public function set table(value:String):void
		{
			_table=value;
			EDIEventProxy.listenerDataRolling(value,this);
		}
		/**
		 * 实现IEDIComponent接口的属性
		 * @return 
		 * 
		 */		
		public function get table():String
		{return _table;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @param value
		 * 
		 */		
		public function set primaryKey(value:String):void
		{_primaryKey=value;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @return 
		 * 
		 */		
		public function get primaryKey():String
		{return _primaryKey;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @param value
		 * 
		 */		
		public function set field(value:String):void
		{_field=value;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @return 
		 * 
		 */		
		public function get field():String
		{return _field;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @param value
		 * 
		 */		
		public function set fieldType(value:String):void
		{_fieldType=value;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @return 
		 * 
		 */		
		public function get fieldType():String
		{return _fieldType;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @param value
		 * 
		 */		
		public function set fieldLength(value:int):void{_fieldLength=value;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @return 
		 * 
		 */		
		public function get fieldLength():int{return _fieldLength;}
		/**
		 * 实现IEDIComponent接口的方法，自动进行数据分发
		 * @param event
		 * 
		 */	
		public function dataRollingHandler(event:EDIDataEvent):void
		{
//			selected=event.value[field]=="true";
			
			if(event.value)
			{
				if(event.value[field]=="1"|| event.value[field]=="true")
				{
					selected=true;
				}
				else
				{
					selected=false;
				}
			}
			else
				selected=false;
		}
		/**
		 * 实现IEDIComponent接口的属性
		 * @return 
		 * 
		 */
		public function get ediValue():String
		{
			return selected?"true":"false";
		}
		/**
		 * 当选择状态发生变化时触发
		 * @param event
		 * 
		 */		
		private function changeHandler(event:Event):void
		{
			if(this.data)
			{
				var obj:Object=this.data;
				selected=obj.selected;
			}
		}
	}
}